package com.park.service.impl;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.park.model.Constants;
import com.park.model.FeeCriterion;
import com.park.model.Park;
import com.park.model.PosChargeData;

public class FeeAddService {
	private static final Log logger = LogFactory.getLog(FeeAddService.class);
	@Autowired
	FormatTime formatTime;
	DecimalFormat df = new DecimalFormat(".00");
	//东云街主
	public static PosChargeData dongyunjiezhu(PosChargeData charge, FeeCriterion criterion, Date exitDate,
			Boolean isQuery, Park park) throws ParseException {
		criterion.setFreemins(30);
		Date enterDate = charge.getEntranceDate();
		Date enterDateNew = new Date(enterDate.getTime() + criterion.getFreemins() * 1000 * 60);
		long diff = (long) Math.ceil((exitDate.getTime() - enterDateNew.getTime()) / (1000 * 60f));
		if (diff <= 0) {
			charge.setChargeMoney(0);
			charge.setEntranceDate1(enterDate);
			charge.setExitDate1(new Date());
			return charge;
		}
		int leftMinuts = (int) Math.ceil((diff % (60 * 24f)));
		int days = (int) (diff / (60 * 24f));
		// logger.info(String.format("车牌号:%s - 停车时长：%d 天 %d 分钟",
		// charge.getCardNumber(),days,leftMinuts));
		double money = 0;
		if (leftMinuts <= 30 && leftMinuts > 0) {
			money = 5;
		} else if (leftMinuts > 30 && leftMinuts <= 210) {
			money = 10;
		} else if (leftMinuts > 210 && leftMinuts <= 570) {
			money = 15;
		} else if (leftMinuts > 570) {
			money = 20;
		}
		double chargeMoney = money + days * 20;
		if(charge.getDiscountType() == 1) {
			chargeMoney = 0;
		}else if(charge.getDiscountType() == 2) {
			chargeMoney = chargeMoney - charge.getDiscount() > 0  ? chargeMoney - charge.getDiscount() : 0;
		}else if(charge.getDiscountType() == 3) {
			chargeMoney = charge.getDiscount();
		}
		logger.info(String.format("车牌号:%s - 停车时长：%d 天 %d 分钟, 停车费：%.1f", charge.getCardNumber(), days, leftMinuts,
				chargeMoney));
		charge.setExitDate1(exitDate);
		charge.setChargeMoney(chargeMoney);
		return charge;
	}
	
	//东云街新计费标准
		public static PosChargeData dongyunjie515(PosChargeData charge, FeeCriterion criterion, Date exitDate,
				Boolean isQuery, Park park) throws ParseException {
			criterion.setFreemins(30);
			Date enterDate = charge.getEntranceDate();
			Date enterDateNew = new Date(enterDate.getTime() + criterion.getFreemins() * 1000 * 60);
			long diff = (long) Math.ceil((exitDate.getTime() - enterDateNew.getTime()) / (1000 * 60f));
			if (diff <= 0) {
				charge.setChargeMoney(0);
				charge.setEntranceDate1(enterDate);
				charge.setExitDate1(new Date());
				return charge;
			}
			int leftMinuts = (int) Math.ceil((diff % (60 * 24f)));
			int days = (int) (diff / (60 * 24f));
			// logger.info(String.format("车牌号:%s - 停车时长：%d 天 %d 分钟",
			// charge.getCardNumber(),days,leftMinuts));
			double money = 0;
			if (leftMinuts <= 30 && leftMinuts > 0) {
				money = 5;
			} else if (leftMinuts > 30 && leftMinuts <= 210) {
				money = 10;
			} else if (leftMinuts > 210 && leftMinuts <= 690) {
				money = 15;
			}  else if (leftMinuts > 690) {
			money = 20;
		}
			double chargeMoney = money + days * 20;
			if(charge.getDiscountType() == 1) {
				chargeMoney = 0;
			}else if(charge.getDiscountType() == 2) {
				chargeMoney = chargeMoney - charge.getDiscount() > 0  ? chargeMoney - charge.getDiscount() : 0;
			}else if(charge.getDiscountType() == 3) {
				chargeMoney = charge.getDiscount();
			}
			logger.info(String.format("车牌号:%s - 停车时长：%d 天 %d 分钟, 停车费：%.1f", charge.getCardNumber(), days, leftMinuts,
					chargeMoney));
			charge.setExitDate1(exitDate);
			charge.setChargeMoney(chargeMoney);
			return charge;
		}
	//东云街4-8
		public static PosChargeData dongyunjie48(PosChargeData charge, FeeCriterion criterion, Date exitDate,
				Boolean isQuery, Park park) throws ParseException {
			criterion.setFreemins(30);
			Date enterDate = charge.getEntranceDate();
			Date enterDateNew = new Date(enterDate.getTime() + criterion.getFreemins() * 1000 * 60);
			long diff = (long) Math.ceil((exitDate.getTime() - enterDateNew.getTime()) / (1000 * 60f));
			if (diff <= 0) {
				charge.setChargeMoney(0);
				charge.setEntranceDate1(enterDate);
				charge.setExitDate1(new Date());
				return charge;
			}
			int leftMinuts = (int) Math.ceil((diff % (60 * 24f)));
			int days = (int) (diff / (60 * 24f));
			// logger.info(String.format("车牌号:%s - 停车时长：%d 天 %d 分钟",
			// charge.getCardNumber(),days,leftMinuts));
			double money = 0;
			if (leftMinuts <= 30 && leftMinuts > 0) {
				money = 5;
			} else if (leftMinuts > 30 && leftMinuts <= 210) {
				money = 10;
			} else if (leftMinuts > 210) {
				int hourstoday=(int) Math.ceil((leftMinuts-210)/60f);
				money = 10+hourstoday*2.5;
			}
			if (money>40) {
				money=40;
			}
			double chargeMoney = money + days * 40;
			if(charge.getDiscountType() == 1) {
				chargeMoney = 0;
			}else if(charge.getDiscountType() == 2) {
				chargeMoney = chargeMoney - charge.getDiscount() > 0  ? chargeMoney - charge.getDiscount() : 0;
			}else if(charge.getDiscountType() == 3) {
				chargeMoney = charge.getDiscount();
			}
			logger.info(String.format("车牌号:%s - 停车时长：%d 天 %d 分钟, 停车费：%.1f", charge.getCardNumber(), days, leftMinuts,
					chargeMoney));
			charge.setExitDate1(exitDate);
			charge.setChargeMoney(chargeMoney);
			return charge;
		}
		
		//航运中心
		public  PosChargeData tengfeidasha(PosChargeData charge,FeeCriterion criterion,Date exitDate,Boolean isQuery) throws ParseException {
			if (exitDate == null) {
				exitDate = new Date();
			}
			charge.setExitDate1(exitDate);

			// 入场免费时间处理
			int freeMinutes = criterion.getFreemins();

			Date entranceDate = new Date(charge.getEntranceDate().getTime() + freeMinutes * 60 * 1000);

			String startTime = new SimpleDateFormat(Constants.DATEFORMAT).format(entranceDate);
			String endTime = new SimpleDateFormat(Constants.DATEFORMAT).format(exitDate);
			String nightStartHour = "20";
			String nightEndHour = "8";
			if (criterion.getNightstarttime() != null && criterion.getNightendtime() != null) {
				nightStartHour = criterion.getNightstarttime().split(":")[0];
				nightEndHour = criterion.getNightendtime().split(":")[0];
			}
			if (entranceDate.getTime() > exitDate.getTime()) {
				charge.setChargeMoney(0);
				return charge;
			}
			double money = 0;

			double step1Price = criterion.getStep1price();
			double step2Price = criterion.getStep3price();
			int step1Minutes = 15;
			int step2minutes = 60;
			step1Minutes=criterion.getTimeoutpriceinterval();
			step2minutes=criterion.getTimeoutpriceinterval2();
			if (charge.getIsLargeCar()) {
				step1Price = criterion.getStep1pricelarge();
				step2Price = criterion.getStep3pricelarge();
			}
				double daymaxfei=12;
				double nightmaxfei=6;
				Map<String, String> dates0 = formatTime.format(startTime, endTime,nightStartHour, nightEndHour);
				double moneytmp=0;
				for (String name : dates0.keySet()) {
					logger.info("计费**车牌号:" + charge.getCardNumber() + "时间:" + name + "-" + dates0.get(name));
					Date startDate = new SimpleDateFormat(Constants.DATEFORMAT).parse(name);
					Date endDate = new SimpleDateFormat(Constants.DATEFORMAT).parse(dates0.get(name));
					String startd = name.substring(11, 13);
					if (Integer.parseInt(startd) >= Integer.parseInt(nightEndHour)
							&& Integer.parseInt(startd) < Integer.parseInt(nightStartHour)) {
						long diffs = endDate.getTime() - startDate.getTime();
						double hours = Math.ceil((double) diffs / (1000 * 60 * step1Minutes));
						if (hours * step1Price>daymaxfei) {
							moneytmp+=daymaxfei;
						}
						else {
							moneytmp += hours * step1Price;
						}					
						logger.info("计费**车牌号:" + charge.getCardNumber() + "费用:" + hours * step1Price);
					} else {
						long diffs = endDate.getTime() - startDate.getTime();
						double hours = Math.ceil((double) diffs / (1000 * 60 * step2minutes));
						if (hours * step2Price>nightmaxfei) {
							moneytmp+=nightmaxfei;
						}
						else {
							moneytmp += hours * step2Price;
						}
						logger.info("计费**车牌号:" + charge.getCardNumber() + "费用:" + hours * step2Price);
					}
					logger.info("计费**车牌号:" + charge.getCardNumber() + "累积费用:" + moneytmp);
				}
				if (moneytmp>criterion.getMaxexpense()) {
					logger.info(charge.getCardNumber()+"达到一天最大金额"+moneytmp+"to"+criterion.getMaxexpense());
					moneytmp=criterion.getMaxexpense();
				}
				money+=moneytmp;
			
					 
			charge.setChargeMoney(Double.valueOf(df.format(money)));
			return charge;
		}
}
